package com.wuyangnju.healthclub.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.wuyangnju.healthclub.entity.Person;
import com.wuyangnju.healthclub.entity.Single;
import com.wuyangnju.healthclub.service.PersonService;
import com.wuyangnju.healthclub.service.SingleService;

@Controller
public class SingleController {

	@Resource
	PersonService personService;

	@Resource
	SingleService singleService;

	@RequestMapping(value = { "/resource/singles.delete" })
	public ModelAndView delete(@RequestParam String cardId) {
		Single single = singleService.get(cardId);
		singleService.delete(single);
		return new ModelAndView(new RedirectView("/resource/singles.get", true));
	}

	@RequestMapping(value = { "/resource/singles.get" })
	public ModelAndView get() {
		return new ModelAndView("single", "singles", singleService.get());
	}

	@RequestMapping(value = { "/resource/singles.put" })
	public ModelAndView put(@RequestParam String cardId,
			@RequestParam String name, @RequestParam String sex,
			@RequestParam int age, @RequestParam String address) {

		if (("".equals(cardId)) || (cardId == null)) {
			Person person = new Person(name, sex, age, address);
			personService.post(person);
			Single single = new Single(person);
			singleService.post(single);
		} else {
			Single single = singleService.get(cardId);
			Person person = single.getPerson();
			person.setAddress(address);
			person.setAge(age);
			person.setName(name);
			person.setSex(sex);
			singleService.put(single);
		}

		return new ModelAndView(new RedirectView("/resource/singles.get", true));
	}
}
